package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_heart.bp_list

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bp.hearthealth.pulse.lnapps.databinding.HeartRateItemLayoutBgBinding
import com.bp.hearthealth.pulse.lnapps.bp_db.bp_heartrate.BpHeartRateBean
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener

class BpHeartRateAdapter :
    RecyclerView.Adapter<BpHeartRateAdapter.ItemVH>() {
    private lateinit var onCliclLisener: OnHeartRateItemClickListener
    private var dataList: List<BpHeartRateBean> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            HeartRateItemLayoutBgBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    fun setHeartRateListData(list: List<BpHeartRateBean>) {
        this.dataList = list
        notifyDataSetChanged()
    }

    inner class ItemVH(val binding: HeartRateItemLayoutBgBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: BpHeartRateBean) {
            binding.bean = item
        }
    }

    override fun onBindViewHolder(holder: ItemVH, @SuppressLint("RecyclerView") position: Int) {
        holder.bindData(dataList[position])
        holder.itemView.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                onCliclLisener.onHeartRateItemClick(dataList[position].id)
            }
        })
    }

    fun setOnItemClickLisener(clickListener: OnHeartRateItemClickListener) {
        this.onCliclLisener = clickListener
    }

    interface OnHeartRateItemClickListener {
        fun onHeartRateItemClick(id: Long)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}